package com.dailyblue.project.estate.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.Repair;
import com.dailyblue.project.estate.commons.handler.RepairException;
import com.dailyblue.project.estate.commons.mapper.RepairMapper;
import com.dailyblue.project.estate.commons.util.DateUtil;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.repair.service.RepairService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Resource
    private RepairMapper repairMapper;

    @Override
    public JsonResult addRepair(Repair repair) {
        boolean b = save(repair);
        if (!b) {
            throw new RepairException("添加报修失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findOneRepair(String repairId) {
        Repair repair = getById(repairId);
        return ResultTool.success(repair);
    }

    @Override
    public JsonResult findAllRepairs() {
        List<Repair> list = list();
        return ResultTool.success(list);
    }

    @Override
    public JsonResult deleteRepair(String repairId) {
        boolean b = removeById(repairId);
        if (b) {
            return ResultTool.success("删除成功");
        }
        return ResultTool.fail(201,"删除失败");
    }

    @Override
    public JsonResult updateRepair(Repair repair) {
        UpdateWrapper<Repair> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", repair.getId());
        updateWrapper.set("community_name", repair.getCommunityName());
        updateWrapper.set("community_id", repair.getCommunityId());
        updateWrapper.set("building_name", repair.getBuildingName());
        updateWrapper.set("building_id", repair.getBuildingId());
        updateWrapper.set("owner_name", repair.getOwnerName());
        updateWrapper.set("owner_id", repair.getOwnerId());
        updateWrapper.set("device_name", repair.getDeviceName());
        updateWrapper.set("device_id", repair.getDeviceId());
        updateWrapper.set("description", repair.getDescription());
        updateWrapper.set("update_time", DateUtil.nowDate());
        int i = repairMapper.update(updateWrapper);
        if (i <= 0) {
            return ResultTool.fail(201, "更新失败");
        }
        return ResultTool.success("更新成功");
    }
}
